package com.zw.controller.admin.tools;

import com.zw.annotation.Log;
import com.zw.core.controller.BaseController;
import com.zw.core.domain.AjaxResult;
import com.zw.core.page.TableDataInfo;
import com.zw.entity.Directory;
import com.zw.enumeration.BusinessType;
import com.zw.service.DirectoryService;
import com.zw.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 目录管理
 *
 * @author zw
 */
@RestController
@RequestMapping(("/tools/directory"))
public class PtDirectoryController extends BaseController {


    @Resource
    private DirectoryService directoryService;


    /**
     * 查询目录列表
     *
     * @param directory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:directory:list')")
    @GetMapping("/list")
    public TableDataInfo list(Directory directory) {
        startPage();
        List<Directory> directories = directoryService.selectDirectoryList(directory);
        return getDataTable(directories);
    }


    /**
     * 查看目录详情
     */
    @PreAuthorize("@ss.hasPermi('tools:directory:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(directoryService.selectDirectoryId(id));
    }

    /**
     * 目录下拉框
     * @param directory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:directory:query')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect(Directory directory) {
        List<Directory> directories = directoryService.selectDirectoryList(directory);
        return success(directories);
    }

    /**
     * 新增目录
     */
    @Log(title = "目录管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('tools:directory:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Directory directory) {
        return toAjax(directoryService.save(directory));
    }


    /**
     * 修改目录
     */
    @Log(title = "目录管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('tools:directory:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Directory directory) {
        return toAjax(directoryService.updateById(directory));
    }


    /**
     * 删除目录
     */
    @Log(title = "目录管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('tools:directory:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(directoryService.removeBatchByIds(Arrays.asList(ids)));
    }


    /**
     * 导出目录
     */
    @PreAuthorize("@ss.hasPermi('tools:directory:export')")
    @Log(title = "目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Directory directory) {
        List<Directory> directories = directoryService.selectDirectoryList(directory);
        ExcelUtil<Directory> util = new ExcelUtil<>(Directory.class);
        util.exportExcel(response, directories, "目录数据");
    }
}
